<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller{
	
	//列出用户信息
	public function index(){
		//接收检索数据
		$limits=array();
		if(isset($_POST['is_forbidden'])) $limits['is_forbidden']=$_POST['is_forbidden']+0;
		if(isset($_POST['is_vip'])) $limits['is_vip']=$_POST['is_vip']+0;
		if(isset($_POST['is_seller'])) $limits['is_seller']=$_POST['is_seller']+0;
		if(!empty($_POST['keyword'])){
			$keyword=trim($_POST['keyword']);
			$limits['user_name']=array('like','%'.$keyword.'%');
		}
		//时间查询数据处理
		if (!empty($_POST['create_time1'])) {
			$limits['create_time']=empty($_POST['create_time2'])?array('gt',strtotime($_POST['create_time1'])):array(array('gt',strtotime($_POST['create_time1'])),array('lt',strtotime($_POST['create_time2'].'+1 day')));
		}else if (!empty($_POST['create_time2'])) {
			$limits['create_time']=array('lt',strtotime($_POST['create_time2'].'+1 day'));
		}
		//当前页数默认为第1页
		$p=isset($_GET['p'])?$_GET['p']:1;
		$pnum=5;
		//分页查询
		$usermodel=M('user');
		$users=$usermodel
			->join('tp_userinfo i on tp_user.user_id=i.user_id')
			->field('i.user_id,user_name,is_forbidden,is_vip,is_seller,create_time,last_time')
			->where($limits)
			->page($p,$pnum)
			->order('i.user_id')
			->select();
		//时间数据处理
		$users=$this->editTime($users);
		//获取总条数
		$count=$usermodel
			->join('tp_userinfo i on tp_user.user_id=i.user_id')
			->where($limits)
			->count();
		//实例化分页对象
		$page=new \Think\Page($count,$pnum,$p);
		//获取分页信息
		$show=$page->show();
		//发送分页信息到模版
		$limits['num']=$count;
		$this->assign("limits",$limits);
		$this->assign("pageinfo",$show);
		//发送查询结果到页面
		$this->assign('users',$users);
		$this->display();	
	}
	
	
	//显示修改(详细)页面
	public function edit(){
		if (IS_POST) {
			$infomodel=new \Model\UserinfoModel();
			//接收数据
			$datainfo=$infomodel->create();
			//判断是否验证通过
			if ($datainfo) {
				$datauser['user_id']=$datainfo['user_id'];
				$datauser['user_name']=trim($_POST['user_name']) ;
				if (!empty($_POST['pwd'])) $datauser['pwd']=trim($_POST['pwd']) ;
				//处理数据
				$datauser=deepspecialchars($datauser);
				//开启事务
				$infomodel->startTrans();
				//修改数据
				if (M('User')->save($datauser)!==false&&$infomodel->save($datainfo)!==false) {
					$infomodel->commit();
					$this->success('修改成功',U('User/index'));
				}else{
					$infomodel->rollback();
					$this->error("修改失败!");
				}
			}else{
				//验证失败
				$this->assign('errorinfo',$infomodel->getError());
				$user=$_POST;
				$this->assign('user',$user);
				$this->display();
			}
		}else{
			//显示修改主页
			$user_id=$_GET['user_id']+0;
			$usermodel=M('user');
			$user=$usermodel
				->join('tp_userinfo i on tp_user.user_id=i.user_id')
				->field('i.*,user_name')
				->where("i.user_id=$user_id")
				->find();
			//处理时间
			$user=$this->editTime($user);
			//分配显示
			$this->assign('user',$user);
			$this->display();
		}
	}
	
	/*//显示修改(详细)页面
	public function edit(){
		$user_id=$_GET['user_id']+0;
		$usermodel=M('user');
		$user=$usermodel
			->join('tp_userinfo i on tp_user.user_id=i.user_id')
			->field('i.*,user_name')
			->where("i.user_id=$user_id")
			->find();
		//处理时间
		$user['create_time']=date("Y-m-d H:i:s", $user['create_time']);
		if ($user['last_time']) $user['last_time']=date("Y-m-d H:i:s", $user['last_time']);
		//分配显示
		$this->assign('user',$user);
		$this->display();
	}
	//修改信息
	public function update(){
		//接收数据
		$datauser['user_id']=$datainfo['user_id']= $_POST['user_id'] + 0 ;
		$datauser['user_name']=trim($_POST['user_name']) ;
		if (!empty($_POST['pwd'])) $datauser['pwd']=trim($_POST['pwd']) ;
		$datainfo['user_sex']=trim($_POST['user_sex']);
		$datainfo['user_credit']=$_POST['user_credit'] + 0 ;
		$datainfo['user_linkman']=trim($_POST['user_linkman']);
		$datainfo['user_addr']=trim($_POST['user_addr']) ;
		$datainfo['user_qq']= $_POST['user_qq'] + 0 ;
		$datainfo['user_tel']=trim($_POST['user_tel']);
		$datainfo['real_name']=trim($_POST['real_name']);
		$datainfo['id_number']=trim($_POST['id_number']) ;
		//$datainfo['create_time']=isset($_POST['create_time']) ? $_POST['create_time'] + 0 : 0 ;
		//$datainfo['last_time']=isset($_POST['last_time']) ? $_POST['last_time'] + 0 : 0 ;
		$datainfo['is_forbidden']= $_POST['is_forbidden'] + 0;
		$datainfo['is_vip']=$_POST['is_vip'] + 0 ;
		$datainfo['is_seller']= $_POST['is_seller'] + 0;
		//处理数据
		$datauser=deepspecialchars($datauser);
		$datainfo=deepspecialchars($datainfo);
		//开启事务
		$usermodel=M("User");
		$usermodel->startTrans();
		//修改数据
		if ($usermodel->save($datauser)!==false&&M('Userinfo')->save($datainfo)!==false) {
			$usermodel->commit();
			$this->success('修改成功',U('User/index'));
		}else{
			$usermodel->rollback();
			$this->error("修改失败!");
		}
	}*/

	public function delete(){
		$user_id=$_GET['user_id']+0;
		$usermodel=M("User");
		//开启事务
		$usermodel->startTrans();
		if ($usermodel->delete($user_id)==1&&M('Userinfo')->delete($user_id)==1) {
			$usermodel->commit();
			$this->success('删除成功',U('User/index'));
		}else {
			$usermodel->rollback();
			$this->error("删除失败!");
		}
	}

	/**
    * 将时间戳显示为时间
    * @param array $arr 待处理数组
    * @return array
    */
	private function editTime($arr){
		if(!isset($arr[0])){
			$arr['create_time']=date("Y-m-d H:i:s", $arr['create_time']);
			$arr['last_time']=$arr['last_time'] ? date("Y-m-d H:i:s", $arr['last_time']):'';
		}else{
			foreach ($arr as $k => $v) {
				$arr[$k]=$this->editTime($v);
			}
		}
		return $arr;
	}
}